#include"Heap.h"

void HeapTest1()
{
    HP hp;
    HeapInit(&hp);

    //int a[] = {27,15,19,18,28,34,65,49,25,37};
    int a[] = {3,9,2,7,5,4,6};
    int i = 0;
    for(i = 0; i < sizeof(a)/sizeof(int); i++)
    {
        HeapPush(&hp, a[i]);
    }
    HeapPrint(&hp);
    //HeapPush(&hp, 10);
   // HeapPrint(&hp);
   printf("%d \n",HeapTop(&hp));
    HeapPop(&hp);
    HeapPrint(&hp);

}
void HeapSort(int* a, int n)
{
    int i = 0;
   // for(i = 1; i < n; i++)
   // {
   //     AdjustUP(a, i);
   // }

    for(i = (n-1-1)/2; i >= 0; i--)
    {
        AdjustDown(a, n, i);
    }

    int end = n - 1;

    while(end > 0)
    {
        HeapSwap(&a[0],&a[end]);
       AdjustDown(a, end, 0);
        end--;
    }

}
void HeapTest2()
{

    HP hp;
    HeapInit(&hp);

    int a[] = {27,15,19,18,28,34,65,49,25,37};
    int i = 0;
   // for(i = 0; i < sizeof(a)/sizeof(int); i++)
   // {
   //     HeapPush(&hp, a[i]);
   // }
    HeapSort(a, 10);
    for(i = 0; i<10; i++)
    {
        printf("%d ", a[i]);
    }

    //HeapPrint(&hp);
   // while(!HeapEmpty(&hp))
   // {
   //     printf("%d ",HeapTop(&hp));
   //     HeapPop(&hp);
   // }
   printf("\n"); 
}
void HeapTest3()
{
    int a[] = {6,1,2,3,4,5};
    int k = 3;
    int* mykin = (int*)malloc(k*sizeof(a[0]));
    assert(mykin);
    int  i = 0;
    for(i = 0; i < k; i++)
    {
        mykin[i] = a[i];
    }

    for(i =(k-1-1)/2; i >=0; i--)
    {
        AdjustDown(mykin, k, i);
    }

    for(i = k; i < 6; i++)
    {
      if(a[i]>mykin[0])
        {
           mykin[0] = a[i];
          AdjustDown(mykin,k,0);
        }
     }
    for(i = 0;i<3;i++)
    {
        printf("%d ",mykin[i]);
    }

}
int main()
{
    HeapTest3();
    return 0;
}
